import { format } from 'date-fns';

/**
 * Gets current device system date.
 * @returns The date instance.
 */
export function getCurrentDate(): Date {
  return new Date();
}

/**
 * Clones the input date and returns a new Date instance with the same value.
 * @param date The source Date instance.
 * @returns The new Date instance with the same value with input Date.
 */
export function cloneDate(date: Date): Date {
  return new Date(date.getTime());
}

/**
 * Adds specific seconds to provided Date instance and returns as a new Date instance.
 * @param date The source Date instance.
 * @param seconds The number of seconds will be added to the provided Date intance.
 * @returns The new Date instance with added seconds to the provided Date.
 */
export function addSeconds(date: Date, seconds: number): Date {
  // Clones the input Date instance.
  const newDate = cloneDate(date);

  // Adds the specific seconds to the input Date.
  newDate.setSeconds(newDate.getSeconds() + seconds);

  // Returns the new Date instance.
  return newDate;
}

/**
 * Adds specific minutes to provided Date instance and returns as a new Date instance.
 * @param date The source Date instance.
 * @param minutes The number of minutes will be added to the provided Date intance.
 * @returns The new Date instance with added seconds to the provided Date.
 */
export function addMinutes(date: Date, minutes: number): Date {
  // Clones the input Date instance.
  const newDate = cloneDate(date);

  // Adds the specific minutes to the input Date.
  newDate.setMinutes(newDate.getMinutes() + minutes);

  // Returns the new Date instance.
  return newDate;
}

/**
 * Adds specific hours to provided Date instance and returns as a new Date instance.
 * @param date The source Date instance.
 * @param hours The number of hours will be added to the provided Date intance.
 * @returns The new Date instance with added hours to the provided Date.
 */
export function addHours(date: Date, hours: number): Date {
  // Clones the input Date instance.
  const newDate = cloneDate(date);

  // Adds the specific hours to the input Date.
  newDate.setHours(newDate.getHours() + hours);

  // Returns the new Date instance.
  return newDate;
}

/**
 * Parses date string to date.
 * @param dateString  The date string.
 * @returns Date instance.
 */
export function parseDate(dateString: string): Date {
  return new Date(Date.parse(dateString));
}

/**
 * Formats input date with provided format.
 */
export function formatDate(date: Date, dateFormat: string): string {
  return format(date, dateFormat);
}
